package com.by.yz.Register;

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.by.yz.App;
import com.by.yz.activity.regitser.RegisterInfoActivity;
import com.by.yz.loginUtil.Constants;

import org.json.JSONObject;

import java.net.CookieStore;
import java.net.HttpCookie;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by wlx on 6/14/16.
 */
public class RegisterRESTFul {

    private  String api="/api/register";

    public RegisterRESTFul() {
        this.api = api;
    }

    public interface ILoginCb {
        void ok(String response);
        void error(String error);

    }
    public void postjs(Context context,JSONObject a, final ILoginCb cb)   {

        String url = Constants.server_url + api;

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(
                Request.Method.POST, url,a,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        cb.ok(response.toString());
                        Log.e("RegisterRESTFul", "Succes:"+response.toString());
                    }
                },
                new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            error.printStackTrace();
                            cb.error(error.toString());
                            Log.e("RegisterRESTFul", "Error:"+error.toString());
                        }
                    }
            ) {

                @Override
                protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
                    return Response.success(new JSONObject(),
                            HttpHeaderParser.parseCacheHeaders(response));
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    CookieStore cookieJar = App.cookieManager.getCookieStore();
                    List<HttpCookie> cookies = cookieJar.getCookies();
                    for (HttpCookie cookie : cookies) {
                        if(cookie.toString().contains("XSRF-TOKE")){
                            headers.put("X-XSRF-TOKEN", cookie.getValue());
                        }
                    }
                    headers.put("Content-Type", "application/json; charset=utf-8");
                    return headers;
                }
            };
            Volley.newRequestQueue(context).add(jsonObjReq);
        }
    }
